/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.common;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Hamish Morgan
 */
public class WorldModel implements Serializable {

    private Map<String, Player> players;
    private Player it = null;

    public WorldModel(HashMap<String, Player> players, Player it) {
        if (players == null)
            throw new NullPointerException();
        if (it == null)
            throw new NullPointerException();
        this.players = players;
        this.it = it;
    }

    public WorldModel() {
        this.players = new HashMap<String, Player>();
        this.it = null;
    }

    public void addPlayer(Player player) {
        if (player == null)
            throw new NullPointerException();
        if (players.containsKey(player.getName()))
            throw new IllegalArgumentException();

        players.put(player.getName(), player);
    }

    public void removePlayer(Player player) {
        if (player == null)
            throw new NullPointerException();
        if (!players.containsKey(player.getName()))
            throw new IllegalArgumentException();

        players.remove(player.getName());
    }

    public Collection<Player> getPlayers() {
        return players.values();
    }

    public Player getIt() {
        return it;
    }

    public void setIt(Player it) {
        if (it == null)
            throw new NullPointerException();

        this.it = it;
    }

    public boolean isIt(Player player) {
        if (it == null)
            throw new NullPointerException();

        return it.equals(player);
    }

    public void updatePlayer(Player player) {
        if (it == null)
            throw new NullPointerException();

        players.get(player.getName()).setLocation(player.getX(),
                player.getY());
    }

    public void clearIt() {
        it = null;
    }

    public Player getAnyPlayer() {
        if (players.isEmpty())
            return null;
        return players.values().iterator().next();
    }

    public boolean playerExists(String name) {
        return players.containsKey(name);
    }

    public int numPlayers() {
        return players.size();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final WorldModel other = (WorldModel) obj;
        if (this.players != other.players &&
                (this.players == null ||
                !this.players.equals(other.players))) return false;
        if (this.it != other.it &&
                (this.it == null || !this.it.equals(other.it)))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash =
                97 * hash +
                (this.players != null ? this.players.hashCode() : 0);
        hash = 97 * hash + (this.it != null ? this.it.hashCode() : 0);
        return hash;
    }

    
}
